# -*- coding: utf-8 -*-
# @时间：2023/11/2 19:55
# @作者：CJiaの可乐哥
# @文件名：cj_response
# @软件：Pycharm2023
import json
from utils.cj_jwt import DateEncoder

from django.http import HttpResponse


class SuccessResponse(HttpResponse):
    """
    标准响应成功的返回, SuccessResponse(data)或者SuccessResponse(data=data)
    (1)默认code返回200, 不支持指定其他返回码
    """

    def __init__(self, data=None, msg='success', code=200, *args, **kwargs):
        std_data = {
            "code": code,
            "result": data,
            "message": msg,
            "success": True
        }
        data = json.dumps(std_data, cls=DateEncoder)
        super().__init__(data, *args, **kwargs)


class ErrorResponse(HttpResponse):
    """
    标准响应错误的返回,ErrorResponse(msg='xxx')
    (1)默认错误码返回400, 也可以指定其他返回码:ErrorResponse(code=xxx)
    """

    def __init__(self, data=None, msg='error', code=400, *args, **kwargs):
        std_data = {
            "code": code,
            "result": data,
            "message": msg,
            "success": False
        }
        std_data = json.dumps(std_data, cls=DateEncoder)
        super().__init__(std_data, *args, **kwargs)
